package com.fzrskj.cloud.free.user.utils;


import com.fzrskj.cloud.free.user.bean.UserMessage;
import com.fzrskj.cloud.free.user.dao.UserMessageDao;
import ms.core.object.CObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangxinhao 
 * @version 2016-10-11
 */
public class UserMessageUtils extends CObject{
	//private declarations
	private UserMessageDao dao=null;

	//constructor function to initialize database access object
	public UserMessageUtils(){
		dao=new UserMessageDao();
	}

	//to free resource
	@Override
	public void dispose(){
		dao.dispose();
	}
	/**
	 * 获取数据消息
	 * @param msgTime
	 * @return
	 */
	public List<UserMessage> getUserMessageList(Date msgTime){
		List<UserMessage> list = dao.getUserMessageList(msgTime);
		String code = dao.getErrCode();
		this.setErrCode(code);
		return list;
	}
	/**
	 * 删除单个消息
	 * @param id
	 * @return
	 */
	public int delUserMessage(int id){
		int ret = dao.delUserMessage(id);
		this.setErrCode(dao.getErrCode());
		return ret;
	}
	/**
	 * 批量删除
	 * @param messagesIds
	 * @param userName
	 * @return
	 */
	public int delUserMessageList(List<UserMessage> messagesIds, String userName){
		int ret = dao.delUserMessage(messagesIds, userName);
		this.setErrCode(dao.getErrCode());
		return ret;
	}
	/**
	 * 标记已读
	 * @param state
	 * @param userName
	 * @return
	 */
	public int upState(int state, String userName){
		int ret = dao.upState(state, userName);
		this.setErrCode(dao.getErrCode());
		return ret;
	}
	/**
	 * 批量标记已读
	 * @param messageIds
	 * @param state
	 * @return
	 */
	public int upStateList(List<UserMessage> messageIds, int state){
		int ret = dao.updStateList(messageIds, state);
		this.setErrCode(dao.getErrCode());
		return ret;
	}
	/**
	 * 统计用户未读消息数量
	 * @param userName
	 * @return
	 */
	public List<UserMessage> getUserMessageCount(String userName){
		List<UserMessage> list = dao.getUserMessageCount(userName);
		String code = dao.getErrCode();
		this.setErrCode(code);
		return list;
	}
	/**
	 * 向数据库添加一条记录
	 * @param content
	 * @param msgTime
	 * @param msgType
	 * @return
	 */
	public int addUserMessage(String content, Date msgTime, int msgType){
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("content", content);
		values.put("msgTime", msgTime);
		values.put("msgType", msgType);
		int ret = dao.addUserMessage(values);
		this.setErrCode(dao.getErrCode());
		return ret;
	}
	/**
	 * 
	 * @param
	 * @return
	 */
	public int queryUserMessage(int uid, int msgType){
		int ret = dao.queryUserMessage(uid,msgType);
		this.setErrCode(dao.getErrCode());
		return ret;
	}
	/**
	 * 
	 * @param uid
	 * @param msgType
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public List<UserMessage> queryUserMessageList(int uid, int msgType, int page, int pagesize){
		List<UserMessage> list = dao.queryUserMessageList(uid, msgType, page, pagesize);
		this.setErrCode(dao.getErrCode());
		return list;
	}
}